'------------------------------------------------------------------------------
'--- UpdateAssemblyFileVersion.vbs
'------------------------------------------------------------------------------
'--- Author - Jim Sally
'--- Date   - 2007.10.23
'------------------------------------------------------------------------------
'--- This updates the AssemblyFileVersion attribute in the AssemblyInfo.cs file
'--- of a given C# project.  The AssemblyFileVersion is updated to contain the
'--- first two digits of the AssemblyVersion, with YMMDD.HHmm as the last two
'--- digits.
'------------------------------------------------------------------------------
Option Explicit

Dim fileHeader
fileHeader = "UpdateAssemblyFileVersion.vbs :: "

EnsureCommandLineArgumentExists
EnsureAssemblyInfoFileExists WScript.Arguments(0)
UpdateAssemblyFileVersion WScript.Arguments(0)

Private Sub EnsureCommandLineArgumentExists()
    If WScript.Arguments.Count <> 1 Then
       WScript.Echo fileHeader & "You must supply the path to the AssemblyInfo.cs file as the only argument on the command line"
       WScript.Quit 1
    End If
End Sub

Private Sub EnsureAssemblyInfoFileExists(AssemblyInfoFileName)
    Dim fso

    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FileExists(AssemblyInfoFileName) Then
       WScript.Echo fileHeader & "AssemblyInfo.cs does not exist at the path provided -> " & AssemblyInfoFileName
       WScript.Quit 1
    End If

    Set fso = Nothing
End Sub

Private Sub UpdateAssemblyFileVersion(AssemblyInfoFileName)
    Dim assemblyInfo, newAssemblyInfo

    assemblyInfo = GetContentsOfAssemblyInfoFile(AssemblyInfoFileName)
    newAssemblyInfo = UpdateAssemblyInfoText(assemblyInfo)

    If newAssemblyInfo <> assemblyInfo Then
       SaveContentsOfAssemblyInfoFile AssemblyInfoFileName, newAssemblyInfo
    End If
End Sub

Private Function GetContentsOfAssemblyInfoFile(AssemblyInfoFileName)
    Dim fso, file

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile(AssemblyInfoFileName)
    GetContentsOfAssemblyInfoFile = file.ReadAll()
    file.Close

    Set file = Nothing
    Set fso = Nothing
End Function

Private Sub SaveContentsOfAssemblyInfoFile(AssemblyInfoFileName, infoText)
    Dim fso, file, f

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFile(AssemblyInfoFileName)
    
    if (f.attributes & 1) Then WScript.Quit
    
    Set file = fso.CreateTextFile(AssemblyInfoFileName, true)
    file.Write(infoText)
    file.Close

    set f = Nothing
    Set file = Nothing
    Set fso = Nothing
End Sub

Private Function UpdateAssemblyInfoText(AssemblyInfoText)
    Dim posAV, posAFV, posEolAV, posEolAFV, aNAV
    Dim originalAssemblyVersionText, originalAssemblyFileVersionText, newAssemblyFileVersionText

    posAV = InStr(AssemblyInfoText, "AssemblyVersion")
    posAFV = InStr(AssemblyInfoText, "AssemblyFileVersion")

    If Not IsNull(posAV) And Not IsNull(posAFV) And posAV > 0 And posAFV > 0 Then
       posEolAV = InStr(posAV, AssemblyInfoText, "]")
       posEolAFV = InStr(posAFV, AssemblyInfoText, "]")

       originalAssemblyVersionText = Mid(AssemblyInfoText, posAV, posEolAV - posAV)
       originalAssemblyFileVersionText = Mid(AssemblyInfoText, posAFV, posEolAFV - posAFV)

       aNAV = Split(Split(originalAssemblyVersionText, """")(1), ".")
       newAssemblyFileVersionText = "AssemblyFileVersion(""" & GetNewFileVersionNumber(aNAV) & """)"

       If originalAssemblyFileVersionText <> newAssemblyFileVersionText Then
           UpdateAssemblyInfoText = Replace(AssemblyInfoText, originalAssemblyFileVersionText, newAssemblyFileVersionText)
       Else
           WScript.Quit
       End If
    Else
       WScript.Echo fileHeader & "AssemblyVersion or AssemblyFileVersion attribute not found"
       WScript.Echo
       WScript.Quit 1
    End If
End Function

Private Function GetNewFileVersionNumber(versionArray)
    GetNewFileVersionNumber = versionArray(0) & "." & GetVersionYMM() & "." & GetVersionDD() & "." & GetVersionHHmm()
End Function

Private Function GetVersionYMM()
    Dim y, m, d

    y = right(year(now()),1)

    If y = 0 Then y = 10

    m = Right("00" & (m + Month(now)), 2)

    GetVersionYMM = y & m
End Function

Private Function GetVersionDD()
    Dim d

    d = Right("00" & Day(now), 2)

    GetVersionDD = d
End Function

Private Function GetVersionHHmm()
    GetVersionHHmm = Right("00" & Hour(Now()), 2) & Right("00" & Minute(Now()), 2)
End Function

